$:.unshift File.join(File.dirname(__FILE__),"..","lib")

require "test/unit"
require "rubygems"
require "active_support"
require "date_input_parser.rb"

class TestDateInputParser < Test::Unit::TestCase
  
  @dateInputParser
  def setup
    @dateInputParser = DateInputParser.new
  end
  
  def test_back_time_by_months
    assert_equal((Time.now-1.months).year, @dateInputParser.getDateToBeChecked("1m").year)
    assert_equal((Time.now-1.months).month, @dateInputParser.getDateToBeChecked("1m").month)    
    assert_equal((Time.now-1.months).day, @dateInputParser.getDateToBeChecked("1m").day)
    
    assert_equal((Time.now-10.months).year, @dateInputParser.getDateToBeChecked("10m").year)
    assert_equal((Time.now-10.months).month, @dateInputParser.getDateToBeChecked("10m").month)    
    assert_equal((Time.now-10.months).day, @dateInputParser.getDateToBeChecked("10m").day)
    
    
    assert_equal((Time.now-12.months).year, @dateInputParser.getDateToBeChecked("12m").year)
    assert_equal((Time.now-12.months).month, @dateInputParser.getDateToBeChecked("12m").month)    
    assert_equal((Time.now-12.months).day, @dateInputParser.getDateToBeChecked("12m").day)
  end

  def test_back_time_by_days
    assert_equal((Time.now-1.days).year, @dateInputParser.getDateToBeChecked("1d").year)
    assert_equal((Time.now-1.days).month, @dateInputParser.getDateToBeChecked("1d").month)    
    assert_equal((Time.now-1.days).day, @dateInputParser.getDateToBeChecked("1d").day)
    
    assert_equal((Time.now-31.days).year, @dateInputParser.getDateToBeChecked("31d").year)
    assert_equal((Time.now-31.days).month, @dateInputParser.getDateToBeChecked("31d").month)    
    assert_equal((Time.now-31.days).day, @dateInputParser.getDateToBeChecked("31d").day)
    
    
    assert_equal((Time.now-12.days).year, @dateInputParser.getDateToBeChecked("12d").year)
    assert_equal((Time.now-12.days).month, @dateInputParser.getDateToBeChecked("12d").month)    
    assert_equal((Time.now-12.days).day, @dateInputParser.getDateToBeChecked("12d").day)
  end
  
    
  def test_back_time_by_weeks
    assert_equal((Time.now-1.weeks).year, @dateInputParser.getDateToBeChecked("1w").year)
    assert_equal((Time.now-1.weeks).month, @dateInputParser.getDateToBeChecked("1w").month)    
    assert_equal((Time.now-1.weeks).day, @dateInputParser.getDateToBeChecked("1w").day)
    
    assert_equal((Time.now-3.weeks).year, @dateInputParser.getDateToBeChecked("3w").year)
    assert_equal((Time.now-3.weeks).month, @dateInputParser.getDateToBeChecked("3w").month)    
    assert_equal((Time.now-3.weeks).day, @dateInputParser.getDateToBeChecked("3w").day)
    
    
    assert_equal((Time.now-4.weeks).year, @dateInputParser.getDateToBeChecked("4w").year)
    assert_equal((Time.now-4.weeks).month, @dateInputParser.getDateToBeChecked("4w").month)    
    assert_equal((Time.now-4.weeks).day, @dateInputParser.getDateToBeChecked("4w").day)
  end
  
  class MockDateInputParser < DateInputParser
    def getDateToBeChecked(time)
      Time.mktime(2009,4,18)
    end
  end
  
  def test_get_absolute_time
    @dateInputParser = MockDateInputParser.new
    assert_equal("2009-4-18",@dateInputParser.getAbsoluteTime("1m"))
  end
end